//
//  ArticleHeadNaviView.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/5/6.
//  Copyright © 2016年 game. All rights reserved.
//

#import "ArticleHeadNaviView.h"

#define CollectYes  @"YES"

#define colletNo  @"NO"
#import "LoginController.h"
@interface ArticleHeadNaviView ()
@property (weak, nonatomic) IBOutlet UIButton *collectButton;
@property (nonatomic,copy) NSString *filePath;
@property (weak, nonatomic) IBOutlet MBLabelWithFontAdapter *titleLab;
@property (nonatomic,strong) AppDelegate *app;
@property (nonatomic,strong) NSMutableArray *colleAry;
@end

@implementation ArticleHeadNaviView

-(void)awakeFromNib{

    self.app = (AppDelegate *)[UIApplication sharedApplication].delegate;
[self.collectButton setImage:[UIImage imageNamed:@"star"] forState:UIControlStateSelected | UIControlStateHighlighted];

}

-(NSMutableArray *)colleAry{
    if (!_colleAry) {
        _colleAry = [NSMutableArray array];
    }
    
    return _colleAry;
}

-(void)setIsVideo:(NSString *)isVideo{
    _isVideo = isVideo;
   
        self.titleLab.text = @"视频详情";
}

-(void)setIsLogin:(NSString *)isLogin{
    _isLogin = isLogin;
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [NSString stringWithFormat:@"%@/Library/Caches/collect",NSHomeDirectory()];;
    
    [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];//在路径创建文件夹
    
    self.filePath  = [NSString stringWithFormat:@"%@/collect.plist",path];
    
    if (![fileManager fileExistsAtPath:self.filePath]) {
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic writeToFile:self.filePath atomically:YES];
    }
    
    
    if ([_isLogin isEqualToString:@"YES"]) {
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 2.0), dispatch_get_main_queue(), ^{
            
            self.userModel = self.app.userModel;
        });
    }
}

-(void)setUserModel:(UserDataModel *)userModel{
    
    _userModel = userModel;
    
    NSString *url= [NSString stringWithFormat:@"http://school.diaoyur.cn/article/user-collection?uid=%@",self.app.userModel.uid];
    
    [[Manager sharedManager]RequestWhithURL:url AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        
        if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
            [ self.colleAry addObjectsFromArray:responseObject[@"data"][@"collection"]];
            
            NSMutableDictionary *ddd = [NSMutableDictionary dictionaryWithContentsOfFile:self.filePath];
            for (NSDictionary *dic in self.colleAry) {
                
                if ([ddd[self.AtcID] isEqualToString:CollectYes]||[dic[@"aid"] isEqualToString:self.AtcID]) {
                    self.collectButton.selected = YES;
                }
            }
            
        }
    }];
    
    
    
}

- (IBAction)backBtn:(UIButton *)sender {
    
    if ([self.deleget respondsToSelector:@selector(backCLick)]) {
        [self.deleget backCLick];
    }
    
}
- (IBAction)collectBtn:(UIButton *)sender {
    
    if ([self.app.isLogin isEqualToString:@"YES"]) {
        
        if (!sender.selected) {
            [self POSTCollectWhithAction:@"add"];
        }else{
            [self POSTCollectWhithAction:@"cancel"];
        }
    }else{
        [self.deleget collectMaseege:NO Message:@"您还未登录，不能收藏"];
    }

}

#pragma mark---收藏操作请求
-(void)POSTCollectWhithAction:(NSString *)action{
    
    if (self.userModel.uid) {
        NSDictionary *dic = @{@"uid":self.userModel.uid,@"article_id":self.AtcID};
        NSString *url = [NSString stringWithFormat:@"http://school.diaoyur.cn/article/%@-collection",action];
        [[Manager sharedManager]ResponseWhithURL:url AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
            [self.deleget collectMaseege:YES Message:responseObject[@"message"]];
            
            if ([responseObject[@"message"] isEqualToString:@"添加成功"]) {
                
                NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:self.filePath];
                [dic setValue:CollectYes forKey:self.AtcID];
                [dic writeToFile:self.filePath atomically:YES];
                
                self.collectButton.selected = YES;
            }else if ([responseObject[@"message"] isEqualToString:@"取消成功"]){
                self.collectButton.selected = NO;
                NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:self.filePath];
                [dic setValue:colletNo forKey:self.AtcID];
                [dic writeToFile:self.filePath atomically:YES];
            }
        }];
    }
}

- (IBAction)shareClick:(id)sender {
    
    if ([self.deleget respondsToSelector:@selector(ShareClick)]) {
        [self.deleget ShareClick];
    }
}


@end
